using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Emlyn.Common.Tools.Web
{
    public delegate void DictionaryChange();

    public class QueryDictionary: IDictionary<string, string>
    {
        Dictionary<string, string> _internal;

        public QueryDictionary(string aQueryString)
        {
            if (aQueryString == null)
                throw new ArgumentNullException("aQueryString");

            _internal = new Dictionary<string, string>();

            string[] lqueryRemoveQuestionMarkArray = aQueryString.Split('?');

            if (lqueryRemoveQuestionMarkArray.Length < 1)
                throw new ArgumentException("splitting on question mark returned zero length array");

            string[] lqueryArray = lqueryRemoveQuestionMarkArray[lqueryRemoveQuestionMarkArray.Length-1].Split('&');

            foreach (string lqueryItem in lqueryArray)
            {
                string[] lqueryItemArray = lqueryItem.Split('=');

                if (lqueryItemArray.Length == 1)
                {
                    Add(lqueryItemArray[0], null);
                }
                else if (lqueryItemArray.Length >= 2)
                {
                    Add(lqueryItemArray[0], lqueryItemArray[1]);
                }
            }
        }

        private DictionaryChange _change;

        public DictionaryChange Change
        {
            get { return _change; }
            set { _change = value; }
        }

        private void OnChange()
        {
            if (_change != null)
                _change();
        }

        public string GetQueryString()
        {
            string retval = String.Empty;

            foreach (string lkey in _internal.Keys)
            {
                if (!String.IsNullOrEmpty(retval))
                    retval += "&";
                retval += HttpUtility.UrlEncode(lkey) + "=" + HttpUtility.UrlEncode(_internal[lkey]);
            }

            return retval;
        }

        #region IDictionary<string,string> Members

        public void Add(string key, string value)
        {
            _internal.Add(key, value);
            OnChange();
        }

        public bool ContainsKey(string key)
        {
            return _internal.ContainsKey(key);
        }

        public ICollection<string> Keys
        {
            get { return _internal.Keys; }
        }

        public bool Remove(string key)
        {
            return _internal.Remove(key);
            OnChange();
        }

        public bool TryGetValue(string key, out string value)
        {
            return TryGetValue(key, out value);
        }

        public ICollection<string> Values
        {
            get { return _internal.Values; }
        }

        public string this[string key]
        {
            get
            {
                return _internal[key];
            }
            set
            {
                _internal[key] = value;
                OnChange();
            }
        }

        #endregion

        #region ICollection<KeyValuePair<string,string>> Members

        public void Add(KeyValuePair<string, string> item)
        {
            _internal.Add(item.Key, item.Value);
            OnChange();
        }

        public void Clear()
        {
            _internal.Clear();
            OnChange();
        }

        public bool Contains(KeyValuePair<string, string> item)
        {
            string lvalue = null;

            return _internal.TryGetValue(item.Key, out lvalue) && lvalue == item.Value;
        }

        public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
        {
            int larrayPos = arrayIndex;
            foreach (string lkey in _internal.Keys)
            {
                array[larrayPos++] = new KeyValuePair<string, string>(lkey, _internal[lkey]);
            }
        }

        public int Count
        {
            get { return _internal.Count; }
        }

        public bool IsReadOnly
        {
            get { return true; }
        }

        public bool Remove(KeyValuePair<string, string> item)
        {
            return _internal.Remove(item.Key);
            OnChange();
        }

        #endregion

        #region IEnumerable<KeyValuePair<string,string>> Members

        public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
        {
            return _internal.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _internal.GetEnumerator();
        }

        #endregion
    }
}
